package cn.kust.servicesocial.service.impl;

import cn.kust.microtourismcommon.constant.CommonStatusEnum;
import cn.kust.microtourismcommon.constant.SocialConstants;
import cn.kust.microtourismcommon.pojo.dto.MicroTourismUser;
import cn.kust.microtourismcommon.pojo.dto.ResponseResult;
import cn.kust.servicesocial.mapper.FollowMapper;
import cn.kust.servicesocial.pojo.dto.MicrotourismFollow;
import cn.kust.servicesocial.service.IFollowService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.QueryChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.UpdateChainWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.time.LocalDateTime;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Function;

import static cn.kust.microtourismcommon.constant.SocialConstants.*;

/**
 * @author YL
 * @data 2022/7/20 22:12
 **/

@Slf4j
@Service
public class FollowServiceImpl extends ServiceImpl<FollowMapper, MicrotourismFollow> implements IFollowService {

    @Autowired
    FollowMapper followMapper;


    @Override
    public ResponseResult deleteFollowById(Long userId, Long friendId) {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id",userId);
        map.put("focus_user_id",friendId);

        //取消关注之前判断是否满足条件
        List<Integer> jugeList = followMapper.relationshipList(userId, friendId);
        if (jugeList.get(0) != 2){
            int result = followMapper.deleteByMap(map);

            if (result == 1)
            {
                return ResponseResult.success();
            }
        }else{
            return ResponseResult.fail(CommonStatusEnum.FOLLOW_NONRELATION.getCode(),CommonStatusEnum.FOLLOW_NONRELATION.getValue());
        }


        return ResponseResult.fail(CommonStatusEnum.FOLLW_DELETE_ERROR.getCode(),CommonStatusEnum.FOLLW_DELETE_ERROR.getValue());
    }


    @Override
    public ResponseResult addFollow(Long userId, Long focusUserId) {
        MicrotourismFollow followDTO = new MicrotourismFollow();
        followDTO.setUserId(userId);
        followDTO.setFocusUserId(focusUserId);
        //插入之前判断是否满足条件
        List<Integer> jugeList = followMapper.relationshipList(userId, focusUserId);
        //无具体关系，直接添加关系
        if (jugeList.size() == 0){
            int result = followMapper.insert(followDTO);
            if (result == 1){
                return ResponseResult.success();
            }
        }
        if (jugeList.get(0) != 1){
            int result = followMapper.insert(followDTO);
            if (result == 1){
                return ResponseResult.success();
            }
        }else {
            return ResponseResult.fail(CommonStatusEnum.FOLLOW_ALLREADY_FOLLOWED.getCode(),CommonStatusEnum.FOLLOW_ALLREADY_FOLLOWED.getValue());
        }



        return ResponseResult.fail(CommonStatusEnum.FOLLW_INSRTE_ERROR.getCode(),CommonStatusEnum.FOLLW_INSRTE_ERROR.getValue());
    }


    @Override
    public ResponseResult findFollowById(Long userId) {
        List<Long> followList = followMapper.findFollowList(userId);

        return ResponseResult.success(followList);
    }


    @Override
    public ResponseResult findFansById(Long userId) {
        List<Long> fanList = followMapper.findFanList(userId);
        return ResponseResult.success(fanList);
    }

    @Override
    public ResponseResult followRelation(Long userId, Long focusUserId) {
        List<Integer> jugeList = followMapper.relationshipList(userId, focusUserId);

        if (jugeList.size() != FOUCS_FOLLOWED){
            // A -> B
            if (jugeList.get(0).equals(USER_FOLLOWED)){
                return ResponseResult.success(USER_FOLLOWED);
            }else{
                // B -> A
                return ResponseResult.success(FOUCS_FOLLOWED);
            }

        }else{
            // A <-> B

            return ResponseResult.success(BOTH_FOLLOWED);
        }

    }


}
